<?php
/**
 * CK 开发框架
 * User: Clake
 * Date: 15/7/28
 * Time: 14:52
 */

namespace CK\Api;

use CK\Core\Component;
use CK\Ex\ExWeixin;
use CK\Util\Arr;
use CK\Util\Cookie;
use CK\Util\Http;

/**
 * 微信的API类
 * Class Weixin
 *
 * @package CK\Api
 */
class Weixin extends Component{

    private $_conf = [
        'appid'=>'wx8abae110598eb209',
        'secret'=>'f2f5bf00051d170d369b1a0d4e707173'
    ];

    private $_appid;

    private $_secret;

    public function __construct($conf) {
        if (!$conf) {
            $conf = $this->getConfig();
        }

        if ($conf && is_array($conf)) {
            $this->_conf = Arr::mrg($this->_conf,$conf);
        }

        $this->_appid = $this->_conf['appid'];
        $this->_secret = $this->_conf['secret'];
    }

    private function http($url) {
        $http = Http::get($url);
        if ($http['code'] != 200) {
            throw new ExWeixin(['errcode'=>$http['code'],'errmsg'=>"HTTP获取失败!\n".var_export($http,true)]);
        }
        $data = json_decode($http['content'],true);
        if (isset($data['errcode']) && $data['errcode'] !=0) {
            throw new ExWeixin($data);
        } else {
            return $data;
        }
    }

    /**
     * @return mixed
     */
    public function getAppid() {
        return $this->_appid;
    }

    /**
     * WEB 验证受权
     * @param $redirect_uri
     *
     * @return mixed|string
     */
    public function webAuth($redirect_uri) {
        $access_token = Cookie::inst()->get('wx_auth');
        if (empty($access_token)) {
            $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?';
            $params = [
                'appid'=>$this->_appid,
                'redirect_uri'=>$redirect_uri,
                'response_type'=>'code',
                'scope'=>'snsapi_userinfo',
                'state'=>'ckcore'
            ];
            $query = http_build_query($params);
            $url .= $query.'#wechat_redirect';
            return $url;
        } else {
            return json_decode($access_token,true);
        }
    }

    /**
     * 用受权CODE得到用户ACCESS_TOKEN
     * @param $code
     *
     * @return mixed
     * @throws ExWeixin
     */
    public function getWebAccessToken($code) {
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?';
        $params = [
            'appid'=>$this->_appid,
            'secret'=>$this->_secret,
            'code'=>$code,
            'grant_type'=>'authorization_code'
        ];
        $url .= http_build_query($params);
        $data = $this->http($url);
        Cookie::inst()->set('wx_auth',json_encode($data),CK_NOW+$data['expires_in']);
        return $data;
    }

    /**
     * 得到微信息用户信息
     * @param $access_token
     * @param $openid
     */
    public function getWebUserInfo($access_token,$openid) {
        $url = 'https://api.weixin.qq.com/sns/userinfo?';
        $params = [
            'access_token' => $access_token,
            'openid' => $openid,
            'lang' => 'zh_CN',
        ];

        $url .= http_build_query($params);
    }


    /**
     * 得到公众号全局 ACCESS_TOKEN
     * @return mixed
     * @throws ExWeixin
     */
    public function getAccessToken() {
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->_appid.'&secret='.$this->_secret;
        $data = $this->http($url);
        if (isset($data['errcode'])) {
            throw new ExWeixin($data);
        }
        return $data;
    }

    /**
     * 得到微信的 JSAPI_TICKET
     * @param $access_token
     *
     * @return mixed
     * @throws ExWeixin
     */
    public function getJsapiTicket($access_token) {
        $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$access_token.'&type=jsapi';
        $data = $this->http($url);
        if ($data['errcode'] != 0) {
            throw new ExWeixin($data);
        }
        return $data;
    }

    /**
     * 得到微信的 CARD_TICKET
     * @param $access_token
     *
     * @return mixed
     * @throws ExWeixin
     */
    public function getCardTicket($access_token) {
        $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$access_token.'&type=wx_card';
        $data = $this->http($url);
        if ($data['errcode'] != 0) {
            throw new ExWeixin($data);
        }
        return $data;
    }
}